﻿using System.Linq;
using System;
using System.Collections.Generic;
using Citi.Import.Util;

namespace Citi.GocAttributes.Domain
{
    public partial class LegalVehicle
    {
        private const string LegalVehicleColumnName = "C_DSMT_TN_LVID";
        private const string LegalVehicleDescriptionColumnName = "LVID_PN_DESCR";

        //private const string LegalVehicleColumnName = "LVID";
        //private const string LegalVehicleDescriptionColumnName = "LVID LONG DESCRIPTION";

        public static readonly List<Definition<LegalVehicle>> Definitions = new List<Definition<LegalVehicle>>
            {
                new Definition<LegalVehicle> { ModelProperty = x => x.Code,MaxLength = 10, Required = true, ColumnName = LegalVehicleColumnName, CustomConversionMethod = ValidateCode},
                new Definition<LegalVehicle> { ModelProperty = x => x.Description, MaxLength = 250, Required = true, ColumnName = LegalVehicleDescriptionColumnName},
            };

        private static object ValidateCode(ref LegalVehicle model, string value, List<string> fields, ref string error, List<string> headers = null)
        {
            if (String.IsNullOrEmpty(value))
            {
                error = String.Format(ErrorFieldNull, LegalVehicleColumnName);
                return Conversion.Validation.Failed;
            }

            var domain = CacheFacade.Get(SelectAll<LegalVehicle>).LastOrDefault(x => x.Code == value);

            if (domain != null)
            {
                model = domain;
                model.LastUpdate = DateTime.Now;
                return Conversion.Validation.Validated;
            }

            model.LastUpdate = DateTime.Now;
            model.ReferenceDate = DateTime.Today;
            return value;
        }

        public static void Save(IEnumerable<LegalVehicle> legalVehicles)
        {
            var toSave = legalVehicles.Distinct(new LegalVehicleComparer()).Where(x => x != null && x.Id == 0).ToList();

            if (toSave.Any())
                toSave.First().Save(toSave, 100);

            CacheFacade.Clear(typeof(LegalVehicle));
        }

        public LegalVehicle SelectByCode(string code)
        {
            return SelectAll<LegalVehicle>().FirstOrDefault(x => x.Code == code);
        }
    }

    public class LegalVehicleComparer : IEqualityComparer<LegalVehicle>
    {
        public bool Equals(LegalVehicle x, LegalVehicle y)
        {
            if (ReferenceEquals(x, y)) return true;
            if (ReferenceEquals(x, null) || ReferenceEquals(y, null))
                return false;

            return x.Description == y.Description && x.Code == y.Code && x.Id == y.Id;
        }

        public int GetHashCode(LegalVehicle item)
        {
            if (ReferenceEquals(item, null)) return 0;

            var hashDescription = item.Description == null ? 0 : item.Description.GetHashCode();
            var hashId = item.Description == null ? 0 : item.Id.GetHashCode();

            return hashDescription ^ hashId;
        }
    }
}
